/**************************************************************************************************
  Filename:       zcl_poll_control.h
  Revised:        $Date: 2014-01-15 11:31:23 -0800 (Wed, 15 Jan 2014) $
  Revision:       $Revision: 36854 $

  Description:    This file contains the ZCL Poll Control definitions.


  Copyright (c) 2019, Texas Instruments Incorporated
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

  *  Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

  *  Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.

  *  Neither the name of Texas Instruments Incorporated nor the names of
      its contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**************************************************************************************************/

#ifndef ZCL_POLL_CONTROL_H
#define ZCL_POLL_CONTROL_H

#ifdef __cplusplus
extern "C"
{
#endif

/******************************************************************************
 * INCLUDES
 */
#include "zcl.h"
#include "ti_zstack_config.h"

#ifdef ZCL_POLL_CONTROL
/******************************************************************************
 * CONSTANTS
 */

/*****************************************/
/***  Poll Control Cluster Attributes ***/
/*****************************************/

  // Server Attributes
#define ATTRID_POLL_CONTROL_CHECK_IN_INTERVAL               0x0000   // M, RW, uint32_t
#define ATTRID_POLL_CONTROL_LONG_POLL_INTERVAL              0x0001   // M, R,  uint32_t
#define ATTRID_POLL_CONTROL_SHORT_POLL_INTERVAL             0x0002   // M, R,  uint16_t
#define ATTRID_POLL_CONTROL_FAST_POLL_TIMEOUT               0x0003   // M, RW, uint16_t
#define ATTRID_POLL_CONTROL_CHECK_IN_INTERVAL_MIN           0x0004   // O, R,  uint32_t
#define ATTRID_POLL_CONTROL_LONG_POLL_INTERVAL_MIN          0x0005   // O, R,  uint32_t
#define ATTRID_POLL_CONTROL_FAST_POLL_TIMEOUT_MAX           0x0006   // O, R,  uint16_t


  // Server Attribute Defaults
#define ATTR_DEFAULT_POLL_CONTROL_CHECK_IN_INTERVAL         0x00003840    //  1 hour, in quarterseconds
#define ATTR_DEFAULT_POLL_CONTROL_LONG_POLL_INTERVAL        0x00000014    //  5 seconds, in quarterseconds
#define ATTR_DEFAULT_POLL_CONTROL_SHORT_POLL_INTERVAL       0x0002        //  0.5 seconds, in quarter seconds
#define ATTR_DEFAULT_POLL_CONTROL_FAST_POLL_TIMEOUT         0x0028        // 10 seconds, in quarter seconds
#define ATTR_DEFAULT_POLL_CONTROL_CHECK_IN_INTERVAL_MIN     0x00000028    // 10 seconds, in quarter seconds
#define ATTR_DEFAULT_POLL_CONTROL_LONG_POLL_INTERVAL_MIN    0x0000000c    //  3 seconds, in quarter seconds
#define ATTR_DEFAULT_POLL_CONTROL_FAST_POLL_TIMEOUT_MAX     0x00f0        // 60 seconds, in quarter seconds

#define POLL_CONTROL_CHECK_IN_ABS_MAX                       0x006e0000    // 20 days maximum

  // commands generated by server
#define COMMAND_POLL_CONTROL_CHECK_IN                                    0x00     // M, no payload

  // commands generated by client
#define COMMAND_POLL_CONTROL_CHECK_IN_RSP                                0x00     // M, zclCmdPollControlCheckInRspPayload_t
#define COMMAND_POLL_CONTROL_FAST_POLL_STOP                              0x01     // M, no payload
#define COMMAND_POLL_CONTROL_SET_LONG_POLL_INTERVAL                      0x02     // O, zclCmdSetLongPollIntervalPayload_t
#define COMMAND_POLL_CONTROL_SET_SHORT_POLL_INTERVAL                     0x03     // O, zclCmdSetShortPollIntervalPayload_t



/*******************************************************************************
 * TYPEDEFS
 */

/*** ZCL Poll Control Cluster: Check In Response payload ***/
typedef struct
{
  bool   startFastPolling;
  uint16_t fastPollTimeout;
} zclCmdPollControlCheckInRspPayload_t;

/*** ZCL Poll Control Cluster: Set Long Poll Interval payload ***/
typedef struct
{
  uint32_t newLongPollInterval;
} zclCmdSetLongPollIntervalPayload_t;

/*** ZCL Poll Control Cluster: Set Short Poll Interval payload ***/
typedef struct
{
  uint16_t newShortPollInterval;
} zclCmdSetShortPollIntervalPayload_t;


/*** Structures used for callback functions ***/
typedef struct
{
  afAddrType_t *srcAddr;
  uint8_t         seqNum;
} zclPollControlCheckIn_t;

typedef struct
{
  uint8_t     startFastPolling;
  uint16_t    fastPollTimeOut;
} zclPollControlCheckInRsp_t;

typedef struct
{
  uint32_t    newLongPollInterval;
} zclPollControlSetLongPollInterval_t;

typedef struct
{
  uint16_t    newShortPollInterval;
} zclPollControlSetShortPollInterval_t;


// This callback is called to process a CheckIn command on a client (ZR), who may configure the server (ZED)
// srcAddr - address/endpoint of ZED that is checking in.
typedef ZStatus_t (*zclPoll_Control_CheckIn_t)( zclPollControlCheckIn_t *pCmd );
typedef ZStatus_t (*zclPoll_Control_CheckInRsp_t)( zclPollControlCheckInRsp_t *pCmd );
typedef ZStatus_t (*zclPoll_Control_FastPollStop_t)( void );
typedef ZStatus_t (*zclPoll_Control_SetLongPollInterval_t)( zclPollControlSetLongPollInterval_t *pCmd );
typedef ZStatus_t (*zclPoll_Control_SetShortPollInterval_t)( zclPollControlSetShortPollInterval_t *pCmd );


// Register Callbacks table entry - enter function pointers for callbacks that
// the application would like to receive
typedef struct
{
  zclPoll_Control_CheckIn_t                           pfnPollControl_CheckIn;
  zclPoll_Control_CheckInRsp_t                        pfnPollControl_CheckInRsp;
  zclPoll_Control_FastPollStop_t                      pfnPollControl_FastPollStop;
  zclPoll_Control_SetLongPollInterval_t               pfnPollControl_SetLongPollInterval;
  zclPoll_Control_SetShortPollInterval_t              pfnPollControl_SetShortPollInterval;
} zclPollControl_AppCallbacks_t;


/******************************************************************************
 * FUNCTION MACROS
 */

/******************************************************************************
 * VARIABLES
 */

/******************************************************************************
 * FUNCTIONS
 */

/*
 * Register for callbacks from this cluster library
 */
extern ZStatus_t zclPollControl_RegisterCmdCallbacks( uint8_t endpoint, zclPollControl_AppCallbacks_t *callbacks );


/*
 * @fn      zclPollControl_Send_CheckIn
 *
 * @brief   Call to send out Poll Control CheckIn command from ZED to ZR/ZC. The Rsp
 *          will indicate whether to stay awake or go back to sleep.
 *
 * @param   srcEP - Sending application's endpoint
 * @param   dstAddr - where you want the message to go
 * @param   disableDefaultRsp - whether to disable the Default Response command
 * @param   seqNum - sequence number
 *
 * @return  ZStatus_t
 */
extern ZStatus_t zclPollControl_Send_CheckIn( uint8_t srcEP, afAddrType_t *dstAddr,
                                              uint8_t disableDefaultRsp, uint8_t seqNum );

/*********************************************************************
 * @fn      zclPollControl_Send_CheckInRsp
 *
 * @brief   Call to send out Poll Control CheckInRsp. This will tell the ZED
 *          who just checked in to stay awake or to sleep.
 *
 * @param   srcEP - Sending application's endpoint
 * @param   dstAddr - where you want the message to go
 * @param   startFastPolling - tell ZED to stay awake and poll at fast rate
 * @param   fastPollTimeout - number of 1/4 seconds for ZED to stay awake and fast poll
 * @param   disableDefaultRsp - whether to disable the Default Response command
 * @param   seqNum - sequence number
 *
 * @return  ZStatus_t
 */
extern ZStatus_t zclPollControl_Send_CheckInRsp( uint8_t srcEP, afAddrType_t *dstAddr,
                                                 uint8_t startFastPolling, uint16_t fastPollTimeout,
                                                 uint8_t disableDefaultRsp, uint8_t seqNum );

/*********************************************************************
 * @fn      zclPollControl_Send_FastPollStop
 *
 * @brief   Call to send out Poll Control FastPollStop. This will tell the ZED
 *          to stop fast polling and go back to sleep at the long poll rate.
 *
 * @param   srcEP - Sending application's endpoint
 * @param   dstAddr - where you want the message to go
 * @param   disableDefaultRsp - whether to disable the Default Response command
 * @param   seqNum - sequence number
 *
 * @return  ZStatus_t
 */
ZStatus_t zclPollControl_Send_FastPollStop( uint8_t srcEP, afAddrType_t *dstAddr,
                                            uint8_t disableDefaultRsp, uint8_t seqNum );

/*********************************************************************
 * @fn      zclPollControl_Send_SetLongPollInterval
 *
 * @brief   Call to send out a Poll Control Set Long Poll Interval command
 *
 * @param   srcEP - Sending application's endpoint
 * @param   dstAddr - where you want the message to go
 * @param   newLongPollInterval - new long poll interval in 1/4 seconds
 * @param   disableDefaultRsp - whether to disable the Default Response command
 * @param   seqNum - sequence number
 *
 * @return  ZStatus_t
 */
ZStatus_t zclPollControl_Send_SetLongPollInterval( uint8_t srcEP, afAddrType_t *dstAddr,
                                                   uint32_t newLongPollInterval,
                                                   uint8_t disableDefaultRsp, uint8_t seqNum );

/*********************************************************************
 * @fn      zclPollControl_Send_SetShortPollInterval
 *
 * @brief   Call to send out a Poll Control Set Long Poll Interval command
 *
 * @param   srcEP - Sending application's endpoint
 * @param   dstAddr - where you want the message to go
 * @param   newShortPollInterval - new short poll interval in 1/4 seconds
 * @param   disableDefaultRsp - whether to disable the Default Response command
 * @param   seqNum - sequence number
 *
 * @return  ZStatus_t
 */
ZStatus_t zclPollControl_Send_SetShortPollInterval( uint8_t srcEP, afAddrType_t *dstAddr,
                                                    uint16_t newShortPollInterval,
                                                    uint8_t disableDefaultRsp, uint8_t seqNum );

/*********************************************************************
*********************************************************************/
#endif // ZCL_POLL_CONTROL

#ifdef __cplusplus
}
#endif

#endif /* ZCL_POLL_CONTROL_H */

